package com.cookie; /**
 * @author wzm
 * @create 2023-05-10 14:47
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie02")
public class CookieServlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //尝试获取Cookie


        //获取Cookie 获取到一堆存储的cookie
        Cookie[] cookies = request.getCookies();
        //循环找到需要的cookie
      for(Cookie cookie:cookies){
         //获取到cookie的key
          String name = cookie.getName();
          //判断找出我们存储的cookie  名字比对
          if ("username".equals(name)) {
              //找到获取该cookie的值
              String value = cookie.getValue();
              System.out.println("value = " + value);
              break;
          }
      }

    }
}
